package mo.offer_special;

import java.util.HashMap;
import java.util.Map;

public class L010 {

    public int subarraySum(int[] nums, int k) {
        int count = 0;
        int pre = 0;
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, 1);
        for (int i = 0; i < nums.length; i++) {
            pre += nums[i];
            count += map.getOrDefault(pre-k, 0);
            int preCount = map.getOrDefault(pre, 0) + 1;
            map.put(pre, preCount);
        }
        return count;
    }

    public static void main(String[] args) {
        L010 test = new L010();
        System.out.println(test.subarraySum(new int[]{1, 1, 1}, 2));
    }
}
